"""
Created on 17-Aug-2009

@author: dexen
"""

import random

import dexen

ALIVE = "Alive"
DEAD = "Dead"

class MyInd(dexen.Individual):
    def __init__(self, nboxes):
        dexen.Individual.__init__(self)
        self.phenotype = None
        self.width = None
        self.height = None
        self.area = None
        self.state = ALIVE 
        self.genotype = {
            "gamma_x" : range(nboxes),
            "gamma_y" : range(nboxes),
            "orient" : [None] * nboxes,
            "encoding" : [None] * nboxes
        }
        random.shuffle(self.genotype['gamma_x'])
        random.shuffle(self.genotype['gamma_y'])
        for i, box_no in enumerate(self.genotype['gamma_x']):
            self.genotype['encoding'][box_no] = dict(x=i)    
        for i, box_no in enumerate(self.genotype['gamma_y']):
            self.genotype['encoding'][box_no]['y'] = i
        for i in range(nboxes):
            self.genotype['orient'][i] = random.randint(0, 1)        
        
    def kill(self):
        self.state = DEAD
    
    def is_alive(self):
        return self.state == ALIVE
        
    def _rr(self, atr, msg):
        if atr != None:
            return msg
        else: return "!"+msg
        
    def __repr__(self):
        res = "Ind: "
        res += self._rr(self.genotype, "gen ")
        res += self._rr(self.phenotype, "phen ")
        res += "width=%s "%self.width
        res += "height=%s "%self.height
        res += "area=%s "%self.area
        return res
